//API 操作
const API = require('../datas/common.js');

//创建精选页面对象
Page({
    data: {
        noMoreData: false,
        seeMore: false,
        loadMore: false,
        banners: [],
        news: [],
        page: 1,
        pageSize: 10
    },
    onLoad: function () {
        let that = this;
        API.get('/content/index', {
            page: that.data.page,
            pageSize: that.data.pageSize
        }).then(res => {
            let news = res.data.news;
            let banners = res.data.banners;
            let hasNext = res.data.hasNext;
            that.setData({
                noMoreData: !hasNext,
                seeMore: hasNext,
                banners: banners,
                news: news
            })
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    onPullDownRefresh: function () {
        this.getNewsByPage(1, true);
    },
    onReachBottom: function () {
        let that = this;
        if (!that.data.noMoreData) {
            that.setData({
                seeMore: false,
                loadMore: true
            });
            let page = that.data.page + 1;
            that.getNewsByPage(page, false);
        }
    },
    getNewsByPage: function (page, isPullDown) {
        let that = this;
        API.get('/content/news', {
            page: page,
            pageSize: that.data.pageSize
        }).then(res => {
            if (isPullDown) {
                wx.stopPullDownRefresh();
            }
            let news = res.data.news;
            let hasNext = res.data.hasNext;
            that.setData({
                noMoreData: !hasNext,
                seeMore: hasNext,
                loadMore: false,
                page: page,
                news: isPullDown ? news : that.data.news.concat(news),
            })
        }).catch((res) => {
            if (isPullDown) {
                wx.stopPullDownRefresh();
            }
            API.showToast(res.message);
        });
    },
    onShareAppMessage: function () {
        //分享记录
    }
});